package com.superman.supermarket.client.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ReloadEvent;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.event.PreorderPageEvent;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.utils.ActivityJumpUrl;
import com.superman.supermarket.client.view.orderdetail.OrderDetailView;

/**
 * 订单详情
 * 
 * @author superman
 * @version 2018年8月2日下午5:08:48
 */
public class OrderDetailActivity extends ShopAbstractActivity {

	@Inject
	OrderDetailView orderDetailView;
	private String orderId;

	private void getOrderDetail() {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ORDER_ID", orderId);
		postman.post("order/query2", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {
				if (statusCode.equals("ORDER10001")) {
					// 订单号不存在
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("orderid", orderId);
					placeController.back(map);
				}
			}

			@Override
			public void onBusinessOK(String response) {
				parseByOrderDetailMap(XJsonUtil.getBody(response));
			}
		});

	}

	@Override
	public IPageView getView() {
		return orderDetailView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onActivity(EventBus eventBus) {
		eventBus.addHandler(ReloadEvent.TYPE, new ReloadEvent.ReloadEventHandler() {

			@Override
			public void reload(ReloadEvent event) {
				getOrderDetail();
			}
		});

		eventBus.addHandler(PreorderPageEvent.TYPE, new PreorderPageEvent.PreorderPageEventHandler() {

			@Override
			public void pushToSkuDetailPage(Map<String, Object> map) {
				ActivityJumpUrl.pushToSkuDetailPage(String.valueOf(map.get("spuid")), String.valueOf(map.get("props")));
			}

			@Override
			public void pushToStorePage(String storeid) {
				ActivityJumpUrl.pushToStorePage(storeid);
			}

			@Override
			public void resetCouponId(String obj) {
				// TODO Auto-generated method stub

			}

			@Override
			public void settlementByCoupon(String couponid) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void parseByOrderDetailMap(Map<String, Object> map) {
		orderDetailView.setAddrInfo(map);

		List<Map<String, Object>> storeSkuList = XJsonUtil.getList((JSONArray) map.get("STORE_ORDER"));
		orderDetailView.setStoreSkuList(storeSkuList, orderId);

		orderDetailView.setOrderInfoModule(map);
	}

	@Override
	public void popToRefresh() {
		// TODO Auto-generated method stub
	}

	@Override
	public void popToRefresh(Object contextData) {
		if (contextData instanceof Map) {
			Map map = (Map) contextData;
			if (map.containsKey("orderid") || map.containsKey("REFUND_ID"))
				getOrderDetail();
		}
	}

	@Override
	public void pushToRefresh() {
		orderId = Window.Location.getParameter("orderid");
		getOrderDetail();
	}

}
